package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckGroupDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.CheckGroupService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author hm
 * @version 1.0
 * @date Created in 2024/7/13 17:18
 */
@Service
public class CheckGroupServiceImpl implements CheckGroupService {
    @Resource
    private CheckGroupDao checkGroupDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<CheckGroup> page = checkGroupDao.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public CheckGroup findById(Integer id) {
       CheckGroup checkGroup =  checkGroupDao.findById(id);
       return checkGroup;
    }

    @Override
    public List<Integer> findCheckItemIds(Integer checkGroupId) {
       List<Integer> checkItemIds =  checkGroupDao.findCheckItemIds(checkGroupId);
       return checkItemIds;
    }

    @Override
    public List<CheckItem> findAll() {
      return checkGroupDao.findAll();
    }

    @Override
    @Transactional
    public void add(CheckGroup checkGroup, Integer[] ids) {
        checkGroupDao.add(checkGroup);
        if (ids.length != 0) {
            checkGroupDao.addCheckItem(checkGroup.getId(), ids);
        }
    }

    @Override
    @Transactional
    public void update(CheckGroup checkGroup, Integer[] checkItemIds){
        if (checkItemIds.length != 0){
            checkGroupDao.update(checkGroup);
            checkGroupDao.deleteAssociation(checkGroup.getId());
            checkGroupDao.addCheckItem(checkGroup.getId(),checkItemIds);
        }
    }

    @Override
    @Transactional
    public void delete(Integer checkGroupId) {
        checkGroupDao.deleteAssociation(checkGroupId);
        checkGroupDao.delete(checkGroupId);
    }

    @Override
    public List<CheckGroup> find() {
       List<CheckGroup> checkGroups = checkGroupDao.find();
       return checkGroups;
    }

    @Override
    public List<Integer> findSetmeal(Integer checkGroupId) {
        List<Integer>  setmeals = checkGroupDao.findSetmeal(checkGroupId);
        return setmeals;
    }
}
